package cm309.BookingSystem.FM.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cm309.BookingSystem.common.MyConnection;
import cm309.BookingSystem.FM.FM_Booking;;


public abstract class CreateBKFMAction implements Action {

	public void execute(HttpServletRequest request, HttpServletResponse response) {
        MyConnection con=new MyConnection();

		
		try {
			
			//update
			PreparedStatement ps=con.getPS(
					"insert into bk_fm_booking (FM_BOOKINGID_1  ,MEMBERID ,RANGEID ," +
					"STATE ,REMARK ,BONUSPOINT ,COMFIRM_EMAIL ) "+
                    " values (?,?,?,?,?,?,?,?)");
			
			//set the Trainer
			FM_Booking b=new FM_Booking();
		
			
			b.setFm_BookingID(request.getParameter("fm_BookingID_1"));		
            b.setMemberID(request.getParameter("trainerID"));
            b.setMemberID(request.getParameter("memberID"));
            b.setRangeID(Integer.parseInt(request.getParameter("rangeID")));
            b.setState("PreBooking");
            b.setRemark(request.getParameter("remark"));
			b.setBonusPoint(Integer.parseInt(request.getParameter("bonusPoint")));
			b.setComfirm_Email(request.getParameter("comfirm_Email"));

			ps.setString(1, b.getFm_BookingID_1());
			ps.setString(3, b.getMemberID());
			ps.setInt(4, b.getRangeID());
		    ps.setString(5, b.getState());
		    ps.setString(6, b.getRemark());
		    ps.setInt(7, b.getBonusPoint());
		    ps.setString(8, b.getComfirm_Email());
			
			int count=ps.executeUpdate();
			if (count!=1) {
				throw new RuntimeException("Insert Booking Error");
			}
			
			con.commit();
	
			 //redirect to the show product!
			PrintWriter w = response.getWriter();
		
			w.println("{'succMsg':'The New PreBooking is successfull'}");
			  			
		} catch (SQLException e) {

			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			
			con.close();
		}

	}

}
